package com.czmh.etc.onlinepubsys.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.czmh.etc.onlinepubsys.utils.httpclient.HttpUtil;
import com.czmh.etc.onlinepubsys.utils.httpclient.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @描述：OCR文字识别工具类 
 * @author： Seven.
 * @创建日期：Jun 8, 2019 12:26:25 AM. 
 * @版本：1.0.0
 */
@Component
public class AliOCRUtil {
    @Value("${aliyunsdk.ocr.idcard.url}")
    private String        idcard;
    @Value("${aliyunsdk.ocr.vehicle.url}")
    private String        vehicle;
    @Value("${aliyunsdk.ocr.appcode}")
    private String        code;
    @Value("${aliyunsdk.ocr.businessLicense.url}")
    private String        businessLicense0;

    private static String idcardUrl;
    private static String vehicleUrl;
    private static String appcode;
    private static String businessLicense;

    /**
     *
     * 先把yml值读取到普通属性中再转到静态属性值时面 
     * @PostConstruct修饰的方法会在服务器加载Servlet的时候运行，并且只会被服务器调用一次;
     *
     */
    @PostConstruct
    public void init() {
        idcardUrl = this.idcard;
        vehicleUrl = this.vehicle;
        appcode = this.code;
        businessLicense = businessLicense0;
    }

    /*
     * 获取参数的json对象
     */
    public static JSONObject getParam(int type, String dataValue) {
        JSONObject obj = new JSONObject();
        try {
            obj.put("dataType", type);
            obj.put("dataValue", dataValue);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return obj;
    }
    
    /*public static void main(String[] args) {
        String imgFile = "/Users/sevenchen/Desktop/WechatIMG409.jpeg";
        // 对图像进行base64编码
        String imgBase64 = "";
        try {
            File file = new File(imgFile);
            byte[] content = new byte[(int) file.length()];
            FileInputStream finputstream = new FileInputStream(file);
            finputstream.read(content);
            finputstream.close();
            imgBase64 = new String(encodeBase64(content));
        } catch (IOException e) {
            e.printStackTrace();
//            return;
        }
        System.out.println(imgBase64);
    }*/

    /**
     * @param type    证件类型：1身份证，2行驶证 3营业执照
     * @param bodys   请求参数
     * @return
     */
    public static String card(int type, String bodys) {
        Map<String, String> headers = new HashMap<String, String>();
        // 最后在header中的格式(中间是英文空格)为Authorization: APPCODE xxxxxx
        headers.put("Authorization", "APPCODE " + appcode);
        headers.put("Content-Type", "application/json;charset=UTF-8");
        String cardResult = "";
        try {
            Result result = null;
            if (type == 1) {
                result = HttpUtil.httpPost(idcardUrl, headers, bodys);
            } else if(type == 2) {
                result = HttpUtil.httpPost(vehicleUrl, headers, bodys);
            } else if(type == 3) {
                result = HttpUtil.httpPost(businessLicense, headers, bodys);
            }
            String res = result.getBody();
            JSONObject res_obj = JSON.parseObject(res);
            cardResult = res_obj.toJSONString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return cardResult;
    }
}